package demo2;

import java.util.Arrays;

public class test {
    //transform
    public static void transform(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] *= 2;
        }
    }

    //如数组：[1,2,3,4,5,6]
    //奇数在偶数之前
    //调整后可能是：[1, 5, 3, 4, 2, 6]
    public static void adjust(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while(left < right) {
            while(left < right && arr[left] % 2 != 0) {
                left++;
            }
            while(left < right && arr[right] % 2 == 0) {
                right--;
            }
            swap(arr, left, right);
        }
    }

    private static void swap(int[] arr, int left, int right) {
        int tmp = arr[left];
        arr[left] = arr[right];
        arr[right] = tmp;
    }

    //二分查找(有序)
    public static boolean binaryFind(int[] arr, int key) {
        int left = 0;
        int right = arr.length - 1;
        int mid = 0;
        while(left < right) {
            mid = (left + right) / 2;
            if(arr[mid] < key) {
                left = mid + 1;
            }else if(arr[mid] > key) {
                right = mid - 1;
            }else {
                return true;
            }
        }
        return false;
    }

    //冒泡排序   (从小到大)
    public static void bubbleSort(int[] arr) {
        boolean flag = false;
        for(int i = 0; i < arr.length - 1; i++) {
            flag = false;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                    flag = true;//代表当前还没有完全排完序
                }
            }
            if(flag == false) {
                break;
            }
        }
    }

    //输入：nums = [2,7,11,15], target = 9
    //
    //输出：[0,1]
    //
    //解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。

    public static int[] findTarget(int[] nums, int target) {
        int[] ret = new int[2];
        for(int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if(nums[i] + nums[j] == target) {
                    ret[0] = i;
                    ret[1] = j;
                    break;
                }
            }
            if(ret != null) {
                break;
            }
        }
        return ret;
    }

    //只出现一次的数字
    public static int oneTimeNumber(int[] arr) {

        int ret = 0;
        for (int i = 0; i < arr.length; i++) {
            ret ^= arr[i];
        }
        return ret;
    }

    //输入：[2,2,1,1,1,2,2]
    //输出：2

    public static int moreTimeNum(int[] arr) {
        int winner = arr[0]; //擂主
        int count = 1;       //上擂台，与自己同队的+1，不同对-1
        for(int i = 1; i < arr.length; i++) {
            if(arr[i] == winner) {
                count++;
            }else {
                count--;
                if(count == 0) {
                    winner = arr[i];
                    count = 1;
                }
            }
        }
        return winner;
    }

    //输入：arr = [1,2,34,3,4,5,7,23,12]
    //
    //输出：true
    //
    //解释：存在连续三个元素都是奇数的情况，即 [5,7,23]
    public static boolean threeOddNum1(int[] arr) {
        int i = 0;
        while(i < arr.length) {
            while(i < arr.length && arr[i] % 2 == 0) {
                i++;
            }
            //找到第一个奇数 i下标
            int j = i + 1;
            int count = 0;
            while(j < arr.length && arr[j] % 2 == 0){
                count++;
                j++;
            }
            if(count != 0) {
                //i下一个元素不是奇数，所以i++然后重新执行循环
                i++;
                continue;
            }
            if(j >= arr.length) {
                break;
            }
            //找到第二个奇数
            int k = i + 2;
            count = 0;
            while(k < arr.length && arr[k] % 2 == 0){
                count++;
                k++;
            }
            if(count != 0) {
                //k下一个元素不是奇数，所以i++然后重新执行循环
                i++;
                continue;
            }
            if(k >= arr.length) {
                break;
            }
            return true;
        }
        return false;
    }
    public static boolean threeOddNum2(int[] arr) {
        int i = 0;
        int k = 1;
        while(i < arr.length) {
            while(i < arr.length && arr[i] % 2 == 0) {
                i++;
            }
            int fla = i;
            for(k = 1; k < 5; k++) {
                int j = i + k;
                int count = 0;
                while(j < arr.length && arr[j] % 2 == 0){
                    count++;
                    j++;
                }
                if(count != 0) {
                    //i下一个元素不是奇数，所以i++然后重新执行循环
                    i++;
                    break;
                }
                if(j >= arr.length) {
                    break;
                }
            }
            if(fla == i && i < arr.length) {
                return true;
            }
        }
        return false;
    }

    public static boolean threeOddNum3(int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] % 2 != 0) {
                count++;
            }else {
                count--;
            }
        }
        if(count == 3) {
            return true;
        }
        return false;
    }
    public static void main(String[] args) {
        int[] arr = new int[] {1,2,34,3,7,5,7,9,12};
        System.out.println(threeOddNum3(arr));
    }
    public static void main7(String[] args) {
        int[] arr = new int[] {2,2,1,1,1,2,2};
        System.out.println(moreTimeNum(arr));
    }

    public static void main6(String[] args) {
        int[] arr= new int[] { 1,4,5,4,1 };
        System.out.println(oneTimeNumber(arr));
    }
    public static void main5(String[] args) {
        int[] arr = new int[] { 5,6,2,38,1,8,6 };
        System.out.println(Arrays.toString(findTarget(arr, 7)));
    }
    public static void main4(String[] args) {
        int[] arr = new int[] { 5,6,2,38,1,8,6};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void main3(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9};
        System.out.println(binaryFind(arr, 3));
    }
    public static void main2(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        adjust(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void main1(String[] args) {
        int[] arr = new int[] {1,2,3};
        transform(arr);
        System.out.println(Arrays.toString(arr));
    }
}
